using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace Microsoft.WindowsAzure.Accelerator
{
    public static class ConsoleProcess
    {
        public static void CopyFolder(Path source, Path target)
        {
            Path commandPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Scripts\copySite.bat");
            Start(commandPath, System.IO.Path.GetFullPath("."), true, source.EnQuote(), target.EnQuote());
        }

        /// <summary>
        /// Starts a new process with the Accelerator environment.
        /// </summary>
        /// <param name="command">The file path.</param>
        /// <param name="workingDir">The working dir.</param>
        /// <param name="waitforExit">if set to <c>true</c> [waitfor exit].</param>
        /// <param name="args">The args.</param>
        /// <returns></returns>
        public static System.Diagnostics.Process Start(Path command, Path workingDir, Boolean waitforExit, params String[] args)
        {
            String a = null;
            args.ForEach(arg => a = (a == null) ? arg : a + " " + arg);
            System.Diagnostics.Process process = new System.Diagnostics.Process
            {
                StartInfo =
                {
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    RedirectStandardInput = false,
                    UseShellExecute = false,
                    CreateNoWindow = false, //true,
                    WindowStyle = ProcessWindowStyle.Normal, // ProcessWindowStyle.Hidden,
                    FileName = command,
                    WorkingDirectory = workingDir,
                    Arguments = a
                }
            };

            //i| Set the environment.
            foreach (var v in ServiceManager.EnvironmentVariables)
                process.StartInfo.EnvironmentVariables[v.Key] = "_" + v.Value;
            
            DataReceivedEventHandler outputHandler = (s, e) =>
            {
                //var consoleOutput = new StringBuilder();
                if (!String.IsNullOrEmpty(e.Data))
                    try
                    {
                        //consoleOutput.Append(e.Data);
                        //Console.WriteLine(consoleOutput);
                        Console.Write(e.Data);
                        Trace.TraceInformation(e.Data);
                    }
                    catch
                    {
                    }
            };

            //i| Set the console output handlers.
            process.ErrorDataReceived += outputHandler;
            process.OutputDataReceived += outputHandler;

            //i| Starting the process
            process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            if (waitforExit)
                process.WaitForExit();
            return process;
        }
    }
}